<?php

namespace app\model;

use think\Model;

class SystemConfig extends Model
{
    protected $name = 'system_configs';
    
    // 设置字段信息
    protected $schema = [
        'id' => 'int',
        'config_key' => 'string',
        'config_value' => 'text',
        'config_name' => 'string',
        'config_type' => 'string',
        'config_group' => 'string',
        'description' => 'string',
        'sort_order' => 'int',
        'is_required' => 'boolean',
        'is_encrypted' => 'boolean',
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];

    // 自动时间戳
    protected $autoWriteTimestamp = true;
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    // 类型转换
    protected $type = [
        'id' => 'integer',
        'sort_order' => 'integer',
        'is_required' => 'boolean',
        'is_encrypted' => 'boolean',
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];

    /**
     * 获取配置值
     * @param string $key 配置键名
     * @param mixed $default 默认值
     * @return mixed
     */
    public static function getConfig($key, $default = null)
    {
        $config = self::where('config_key', $key)->find();
        if (!$config) {
            return $default;
        }
        
        $value = $config->config_value;
        
        // 如果是加密的配置，需要解密
        if ($config->is_encrypted && !empty($value)) {
            $value = self::decrypt($value);
        }
        
        return $value;
    }

    /**
     * 设置配置值
     * @param string $key 配置键名
     * @param mixed $value 配置值
     * @return bool
     */
    public static function setConfig($key, $value)
    {
        $config = self::where('config_key', $key)->find();
        if (!$config) {
            return false;
        }
        
        // 如果是加密的配置，需要加密
        if ($config->is_encrypted && !empty($value)) {
            $value = self::encrypt($value);
        }
        
        $config->config_value = $value;
        return $config->save();
    }

    /**
     * 获取分组配置
     * @param string $group 配置分组
     * @return array
     */
    public static function getConfigsByGroup($group)
    {
        return self::where('config_group', $group)
            ->order('sort_order', 'asc')
            ->select()
            ->toArray();
    }

    /**
     * 批量更新配置
     * @param array $configs 配置数组 [key => value]
     * @return bool
     */
    public static function batchUpdate($configs)
    {
        foreach ($configs as $key => $value) {
            if (!self::setConfig($key, $value)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 加密数据
     * @param string $data
     * @return string
     */
    private static function encrypt($data)
    {
        $key = config('app.jwt_secret', 'dyorder_jwt_secret_key_2024');
        $method = 'AES-256-CBC';
        $iv = substr(hash('sha256', $key), 0, 16);
        
        return base64_encode(openssl_encrypt($data, $method, $key, 0, $iv));
    }

    /**
     * 解密数据
     * @param string $data
     * @return string
     */
    private static function decrypt($data)
    {
        $key = config('app.jwt_secret', 'dyorder_jwt_secret_key_2024');
        $method = 'AES-256-CBC';
        $iv = substr(hash('sha256', $key), 0, 16);
        
        return openssl_decrypt(base64_decode($data), $method, $key, 0, $iv);
    }
} 